/**
 * Created by sun on 2016/9/18.
 */

define(function (require) {
    var app = require('/../js/app');
    require('ui-table');


    var toastr =require('toastr');
    app.useModule("ui.table");

    var datepicker = require('datepicker');
    app.controller('salesman_listCtrl', ['$scope','$http','$filter', '$interval','$timeout',function ($scope, $http,$filter,$interval,$timeout) {
        $scope.ctrlScope=$scope;
        $scope.ctrlScope.itemsearch={};
        $scope.bind={};
        $scope.add={};
        var scopePath = window.location.host;
        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };
        $scope.selectOptions = {
            allowClear: false,
            language : 'zh-CN',
        };
        //$scope.orderParams = {
        //    url: "/admin/sysuser/getGuaranteeList",
        //    ajax:{type:3}
        //
        //};
        //获取前台用户列表
        $scope.getUserList=function(){
            $scope.usernames={};
            console.info($scope.bind.type);
            $http({
                method: 'POST',
                url: "/admin/sysuser/listUser",
                data:{type:$scope.bind.type}
            }).success(function(data) {
                console.log(data)
                $scope.usernames=data;
                $scope.matchMobile= function (id) {
                    for(var i=0;i<data.length;i++){
                        if(data[i].id==id){
                            $scope.bind.mobile=data[i].mobile
                        }
                    }
                }
            }).error(function() {
            });
        };
        $scope.orderParams = {
            url: "/admin/sysuser/listSalesman ",

        };
        datepicker($scope);

        //获取短信验证码
        $scope.getCode=function(id){

            $http({
                method: 'POST',
                url: "/admin/sysUser/bandingSendMessage",
                data:{userId:id},
            }).success(function(data) {
                console.info(data);
                if(data.result=='success'){

                    $scope.Verification.getCodeTest='重新发送';
                    $scope.Verification.time=60;
                    $scope.Verification.isTimeOut=true;

                    $interval(function () {
                        if($scope.Verification.time==0){
                            $scope.Verification.isTimeOut=false;
                            $scope.Verification.getCodeTest='获取短信验证码';
                            $scope.Verification.time='';
                        }else{
                            $scope.Verification.time--;
                        }
                    },1000);
                }else{
                    toastr.error('发送失败');
                    $scope.Verification.isTimeOut=false;
                    $scope.Verification.getCodeTest='获取短信验证码';
                    $scope.Verification.time='';
                }

            }).error(function(data) {
                toastr.error('通讯失败')
            });
        };


        //点击绑定用户
        $scope.bindingUser= function (sysId,type) {
            $scope.bind={};
            $scope.Verification = {};
            $scope.Verification.getCodeTest = '获取短信验证码';
            $scope.Verification.isTimeOut = false;
            $scope.Verification.time = '';
            $scope.bindingModal=!$scope.bindingModal;
            $scope.bind.type=type;
            $scope.getUserList();
            $scope.bind.sysId=sysId;

            console.info($scope.bind.type);
            console.info(1111111111111111111);
        };

        //提交
        $scope.confirm= function () {
            $http({
                method: 'POST',
                url: "/admin/sysuser/bindingUser",
                data:{
                    userId:$scope.bind.userId,
                    sysUserId:$scope.bind.sysId,
                    mobileCode:$scope.bind.mobileCode
                }
            }).success(function(data) {
                console.info(data);
                if(data.result=='success'){
                    toastr.success('绑定成功');
                    $scope.bindingModal = !$scope.bindingModal;
                    $scope.orderParams .reload();
                }else{
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error('绑定失败');
                    }

                    $scope.Verification.isTimeOut2=false;
                    $scope.Verification.getCodeTest2='获取短信验证码';
                    $scope.Verification.time2='';
                }

            }).error(function(data) {});
        };

        //公司列表
        $scope.getCompanyList=function(){
            $scope.companise={};
            $http({
                method: 'POST',
                url: "/admin/report/listCompany",
                data:{}
            }).success(function(data) {
                console.log(data)
                $scope.companise=data;
            }).error(function() {
            });
        };

        //搜索
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDate=$scope.ctrlScope.itemsearch.beginDatex;
            $scope.ctrlScope.itemsearch.endDate= $scope.ctrlScope.itemsearch.endDatex;
            $scope.ctrlScope.itemsearch.beginDate=$filter('date')($scope.ctrlScope.itemsearch.beginDate,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDate=$filter('date')($scope.ctrlScope.itemsearch.endDate,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.type=3;
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={type:'3'};
            $scope.orderParams.reload();

        };


        //新建用户
        $scope.addOrderModal=function(){
            $scope.add={};
            $scope.addModal = !$scope.addModal;
            $scope.getCompanyList();
        };
        //创建提交
        $scope.addSubmit = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            console.info($scope.add.companyId);
            $http({
                method: 'POST',
                url: "/admin/sysuser/add",
                data:{
                    type:3,
                    sysUserName:$scope.add.sysUserName,
                    password:$scope.add.password,
                    companyId:$scope.add.companyId,
                    type:$scope.add.type,
                }
            }).success(function(data) {

                if(data.result=="success"){
                    toastr.success("创建成功！");
                    $scope.addModal = !$scope.addModal;
                    $scope.orderParams.ajaxParam={type:'3'};
                    $scope.orderParams.reload();
                }else{
                    if(data.messageText){
                        toastr.error(data.messageText);
                    }else{
                        toastr.error('创建失败');
                    }
                }

            }).error(function(data) {
            });
        };


        //绑卡
        $scope.card=function(id){
            var userId=id;
            window.location.href='http://'+scopePath+'/admin/accountbank/add/'+userId;

        };


        //认证
        $scope.identify=function(id){
            $scope.identifyModel = !$scope.identifyModel ;

            $scope.add={};
            $scope.add.userId=id;
        };

        //认证提交
        $scope.identifySubmit = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }
            var p={
                realname:$scope.add.realname,
                IdCard:$scope.add.IdCard,
                userId:$scope.add.userId,
                type:3
            };
            console.log(p)
            $http({
                method: 'POST',
                url: "/admin/user/doRealname",
                data:p
            }).success(function(data) {
                console.log(data)
                if(data.result=="success"){
                    toastr.success("认证成功！");
                    $scope.identifyModel = !$scope.identifyModel;
                    $scope.orderParams.ajaxParam={type:'3'};
                    $scope.orderParams.reload();
                }else{
                    toastr.error("认证失败");
                }

            }).error(function(data) {
            });
        };


        //删除
        $scope.deleteUser = function(id) {
            var parm = {
                "sysUserId": id,
            }
            if (confirm("确认删除?")) {
                $http({
                    method: 'POST',
                    url: "/admin/sysUser/deleteSysUser",
                    data: parm,
                }).success(function(data) {
                    console.log(data)
                    if(data.result=='fail'){
                        if(data.messageText){
                            toastr.error(data.messageText)
                        }else{
                            toastr.error('删除失败')
                        }

                    }else{
                        toastr.success('删除成功');
                        $scope.orderParams.ajaxParam={type:'3'};
                        $scope.orderParams.reload();
                    }

                }).error(function(data) {});
            } else {
                return
            }
        };


    }]);


});